package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbBrandMapper;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbBrandExample;
import com.dongdongshop.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public PageResult listBrand(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(null);
        PageInfo<TbBrand> pageInfo = new PageInfo<>(tbBrands);

        PageResult pageResult = new PageResult(pageNum,pageSize,pageInfo.getTotal(),pageInfo.getList(),pageInfo.getPages());
        return pageResult;
    }

    @Override
    public void addBrand(TbBrand tbBrand) {
        tbBrandMapper.insertSelective(tbBrand);
    }

    @Override
    public TbBrand toUpdate(Long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateBrand(TbBrand tbBrand) {
        tbBrandMapper.updateByPrimaryKey(tbBrand);
    }

    @Override
    public void delBrands(List<Long> ids) {
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
        criteria.andIdIn(ids);
        tbBrandMapper.deleteByExample(tbBrandExample);
    }
}
